package com.gjy.concurrency.clazz;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveAction;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-05-18 11:05:55
 */
@Slf4j
public class RecursiveActionTest {


    public void testForRATask1() {
        /*//计算执行时间
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ForkJoinPool pool = null;
        try {
            pool = new ForkJoinPool(10);
            FactoryBatchTask factoryBatchTask = new FactoryBatchTask(sapFactoryBaseList, sapFactoryBaseMapper);
            ForkJoinTask<Void> submit = pool.submit(factoryBatchTask);
            submit.get();
        } catch (InterruptedException e) {
            log.error("插入工厂数据异常(InterruptedException):{}", e.getMessage());
            throw new BusinessException("同步工厂数据异常");
        } catch (ExecutionException e) {
            log.error("插入工厂数据异常(ExecutionException):{}", e.getMessage());
            throw new BusinessException("同步工厂数据异常");
        } finally {
            //关闭线程池
            Objects.requireNonNull(pool).shutdown();
        }
        // 结束时间
        stopWatch.stop();
        log.info("工厂主数据同步完成:size:{},耗时:{}", sapFactoryBaseList.size(), stopWatch.getTotalTimeSeconds());*/
    }

    /**
     * 示例,大数据量插入
     */
    private static final class RATask1 extends RecursiveAction {

        //设置批量处理数据量临界值
        private static final int THRESHOLD = 200;

        /*List<SapFactoryBase> sapFactoryBaseList;

        private SapFactoryBaseMapper sapFactoryBaseMapper;*/

        /*public RATask1(
                List<SapFactoryBase> sapFactoryBaseList,
                SapFactoryBaseMapper sapFactoryBaseMapper) {
            this.sapFactoryBaseList = sapFactoryBaseList;
            this.sapFactoryBaseMapper = sapFactoryBaseMapper;

        }*/

        /**
         * The main computation performed by this task.
         */
        @Override
        protected void compute() {
            /*boolean cutFlag = sapFactoryBaseList.size() <= THRESHOLD;
            if (cutFlag) {

                for (SapFactoryBase item : sapFactoryBaseList) {
                    log.info("item:{}", item.toString());
                    //插入工厂基础数据
                    if (ObjectUtil.isNotNull(item)) {
                        sapFactoryBaseMapper.insertSapFactoryBase(item);
                    }
                }
            } else {
                List<List<SapFactoryBase>> lists = RATask1.averageAssign(sapFactoryBaseList, 2);
                // 递归
                RATask1 task1 = new RATask1(lists.get(0), sapFactoryBaseMapper);
                RATask1 task2 = new RATask1(lists.get(1), sapFactoryBaseMapper);
                // 拆分任务，把任务压入线程队列
                invokeAll(task1, task2);
            }*/
        }

        /**
         * 将一组数据平均分成n组
         *
         * @param source 要分组的数据源
         * @param n      平均分成n组
         */
        public static <T> List<List<T>> averageAssign(List<T> source, int n) {
            List<List<T>> result = new ArrayList<>();
            //(先计算出余数)
            int remainder = source.size() % n;
            //然后是商
            int number = source.size() / n;
            //偏移量
            int offset = 0;
            for (int i = 0; i < n; i++) {
                List<T> value;
                if (remainder > 0) {
                    value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                    remainder--;
                    offset++;
                } else {
                    value = source.subList(i * number + offset, (i + 1) * number + offset);
                }
                result.add(value);
            }
            return result;
        }

    }
}
